package com.example.fileserver_API.Interfaces.Service;

import com.example.fileserver_API.Dao.UploadpicturesMappers;
import com.example.fileserver_API.Entity.PO.uploadpictures;
import com.example.fileserver_API.Entity.VO.APIResult;
import com.example.fileserver_API.Interfaces.IService.IUploadpicturesService;
import com.example.fileserver_API.utils.APIResultUtils;
import com.example.fileserver_API.utils.GetGuid;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Date;

@Service
public class UploadpicturesService implements IUploadpicturesService {
    @Resource
    UploadpicturesMappers _uploadpicturesMappers;

    /**
     * 上传图片的时候添加记录
     * @return APIResult
     */
    @Override
    public APIResult AddPicRecord(String oldname,String newname,double filesize,String filepath,String suffix,String filetype,String resouretype) {
        DecimalFormat df = new DecimalFormat("#.00"); //保留两位小数
        String guid =  GetGuid.ToString();
        uploadpictures uploadpictures = new uploadpictures();
        uploadpictures.setGuid(guid);
        uploadpictures.setSuffix(suffix);
        uploadpictures.setOldfilename(oldname);
        uploadpictures.setNewfilename(newname);
        uploadpictures.setFilesize(Double.parseDouble(df.format(filesize/(1024*1024))));
        uploadpictures.setFilepath(filepath);
        uploadpictures.setFiletype(filetype);
        uploadpictures.setResouretype(resouretype);
        uploadpictures.setCreatetime(new Date());
        int successcount = _uploadpicturesMappers.insert(uploadpictures);
        if (successcount>0){
            System.out.println("记录保存成功");
            return APIResultUtils.success("记录添加成功",guid);
        }
        System.out.println("记录保存失败");
        return APIResultUtils.error("记录添加失败");
    }
}
